﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using AnalyticsGadget.Models;

namespace AnalyticsGadget
{
    public class ChartHelper
    {
        static Dictionary<string, string> _countries;

        static ChartHelper()
        {
            _countries = new Dictionary<string, string>();
            _countries.Add("AFGHANISTAN", "AF");
            _countries.Add("ÅLAND ISLANDS", "AX");
            _countries.Add("ALBANIA", "AL");
            _countries.Add("ALGERIA", "DZ");
            _countries.Add("AMERICAN SAMOA", "AS");
            _countries.Add("ANDORRA", "AD");
            _countries.Add("ANGOLA", "AO");
            _countries.Add("ANGUILLA", "AI");
            _countries.Add("ANTARCTICA", "AQ");
            _countries.Add("ANTIGUA AND BARBUDA", "AG");
            _countries.Add("ARGENTINA", "AR");
            _countries.Add("ARMENIA", "AM");
            _countries.Add("ARUBA", "AW");
            _countries.Add("AUSTRALIA", "AU");
            _countries.Add("AUSTRIA", "AT");
            _countries.Add("AZERBAIJAN", "AZ");
            _countries.Add("BAHAMAS", "BS");
            _countries.Add("BAHRAIN", "BH");
            _countries.Add("BANGLADESH", "BD");
            _countries.Add("BARBADOS", "BB");
            _countries.Add("BELARUS", "BY");
            _countries.Add("BELGIUM", "BE");
            _countries.Add("BELIZE", "BZ");
            _countries.Add("BENIN", "BJ");
            _countries.Add("BERMUDA", "BM");
            _countries.Add("BHUTAN", "BT");
            _countries.Add("BOLIVIA", "BO");
            _countries.Add("BOSNIA AND HERZEGOVINA", "BA");
            _countries.Add("BOTSWANA", "BW");
            _countries.Add("BOUVET ISLAND", "BV");
            _countries.Add("BRAZIL", "BR");
            _countries.Add("BRITISH INDIAN OCEAN TERRITORY", "IO");
            _countries.Add("BRUNEI DARUSSALAM", "BN");
            _countries.Add("BULGARIA", "BG");
            _countries.Add("BURKINA FASO", "BF");
            _countries.Add("BURUNDI", "BI");
            _countries.Add("CAMBODIA", "KH");
            _countries.Add("CAMEROON", "CM");
            _countries.Add("CANADA", "CA");
            _countries.Add("CAPE VERDE", "CV");
            _countries.Add("CAYMAN ISLANDS", "KY");
            _countries.Add("CENTRAL AFRICAN REPUBLIC", "CF");
            _countries.Add("CHAD", "TD");
            _countries.Add("CHILE", "CL");
            _countries.Add("CHINA", "CN");
            _countries.Add("CHRISTMAS ISLAND", "CX");
            _countries.Add("COCOS (KEELING) ISLANDS", "CC");
            _countries.Add("COLOMBIA", "CO");
            _countries.Add("COMOROS", "KM");
            _countries.Add("CONGO", "CG");
            _countries.Add("CONGO, THE DEMOCRATIC REPUBLIC OF THE", "CD");
            _countries.Add("COOK ISLANDS", "CK");
            _countries.Add("COSTA RICA", "CR");
            _countries.Add("CÔTE D'IVOIRE", "CI");
            _countries.Add("CROATIA", "HR");
            _countries.Add("CUBA", "CU");
            _countries.Add("CYPRUS", "CY");
            _countries.Add("CZECH REPUBLIC", "CZ");
            _countries.Add("DENMARK", "DK");
            _countries.Add("DJIBOUTI", "DJ");
            _countries.Add("DOMINICA", "DM");
            _countries.Add("DOMINICAN REPUBLIC", "DO");
            _countries.Add("ECUADOR", "EC");
            _countries.Add("EGYPT", "EG");
            _countries.Add("EL SALVADOR", "SV");
            _countries.Add("EQUATORIAL GUINEA", "GQ");
            _countries.Add("ERITREA", "ER");
            _countries.Add("ESTONIA", "EE");
            _countries.Add("ETHIOPIA", "ET");
            _countries.Add("FALKLAND ISLANDS (MALVINAS)", "FK");
            _countries.Add("FAROE ISLANDS", "FO");
            _countries.Add("FIJI", "FJ");
            _countries.Add("FINLAND", "FI");
            _countries.Add("FRANCE", "FR");
            _countries.Add("FRENCH GUIANA", "GF");
            _countries.Add("FRENCH POLYNESIA", "PF");
            _countries.Add("FRENCH SOUTHERN TERRITORIES", "TF");
            _countries.Add("GABON", "GA");
            _countries.Add("GAMBIA", "GM");
            _countries.Add("GEORGIA", "GE");
            _countries.Add("GERMANY", "DE");
            _countries.Add("GHANA", "GH");
            _countries.Add("GIBRALTAR", "GI");
            _countries.Add("GREECE", "GR");
            _countries.Add("GREENLAND", "GL");
            _countries.Add("GRENADA", "GD");
            _countries.Add("GUADELOUPE", "GP");
            _countries.Add("GUAM", "GU");
            _countries.Add("GUATEMALA", "GT");
            _countries.Add("GUERNSEY", "GG");
            _countries.Add("GUINEA", "GN");
            _countries.Add("GUINEA-BISSAU", "GW");
            _countries.Add("GUYANA", "GY");
            _countries.Add("HAITI", "HT");
            _countries.Add("HEARD ISLAND AND MCDONALD ISLANDS", "HM");
            _countries.Add("HOLY SEE (VATICAN CITY STATE)", "VA");
            _countries.Add("HONDURAS", "HN");
            _countries.Add("HONG KONG", "HK");
            _countries.Add("HUNGARY", "HU");
            _countries.Add("ICELAND", "IS");
            _countries.Add("INDIA", "IN");
            _countries.Add("INDONESIA", "ID");
            _countries.Add("IRAN, ISLAMIC REPUBLIC OF", "IR");
            _countries.Add("IRAQ", "IQ");
            _countries.Add("IRELAND", "IE");
            _countries.Add("ISLE OF MAN", "IM");
            _countries.Add("ISRAEL", "IL");
            _countries.Add("ITALY", "IT");
            _countries.Add("JAMAICA", "JM");
            _countries.Add("JAPAN", "JP");
            _countries.Add("JERSEY", "JE");
            _countries.Add("JORDAN", "JO");
            _countries.Add("KAZAKHSTAN", "KZ");
            _countries.Add("KENYA", "KE");
            _countries.Add("KIRIBATI", "KI");
            _countries.Add("KOREA, DEMOCRATIC PEOPLE'S REPUBLIC OF", "KP");
            _countries.Add("KOREA, REPUBLIC OF", "KR");
            _countries.Add("KUWAIT", "KW");
            _countries.Add("KYRGYZSTAN", "KG");
            _countries.Add("LAO PEOPLE'S DEMOCRATIC REPUBLIC", "LA");
            _countries.Add("LATVIA", "LV");
            _countries.Add("LEBANON", "LB");
            _countries.Add("LESOTHO", "LS");
            _countries.Add("LIBERIA", "LR");
            _countries.Add("LIBYAN ARAB JAMAHIRIYA", "LY");
            _countries.Add("LIECHTENSTEIN", "LI");
            _countries.Add("LITHUANIA", "LT");
            _countries.Add("LUXEMBOURG", "LU");
            _countries.Add("MACAO", "MO");
            _countries.Add("MACEDONIA, THE FORMER YUGOSLAV REPUBLIC OF", "MK");
            _countries.Add("MADAGASCAR", "MG");
            _countries.Add("MALAWI", "MW");
            _countries.Add("MALAYSIA", "MY");
            _countries.Add("MALDIVES", "MV");
            _countries.Add("MALI", "ML");
            _countries.Add("MALTA", "MT");
            _countries.Add("MARSHALL ISLANDS", "MH");
            _countries.Add("MARTINIQUE", "MQ");
            _countries.Add("MAURITANIA", "MR");
            _countries.Add("MAURITIUS", "MU");
            _countries.Add("MAYOTTE", "YT");
            _countries.Add("MEXICO", "MX");
            _countries.Add("MICRONESIA, FEDERATED STATES OF", "FM");
            _countries.Add("MOLDOVA, REPUBLIC OF", "MD");
            _countries.Add("MONACO", "MC");
            _countries.Add("MONGOLIA", "MN");
            _countries.Add("MONTENEGRO", "ME");
            _countries.Add("MONTSERRAT", "MS");
            _countries.Add("MOROCCO", "MA");
            _countries.Add("MOZAMBIQUE", "MZ");
            _countries.Add("MYANMAR", "MM");
            _countries.Add("NAMIBIA", "NA");
            _countries.Add("NAURU", "NR");
            _countries.Add("NEPAL", "NP");
            _countries.Add("NETHERLANDS", "NL");
            _countries.Add("NETHERLANDS ANTILLES", "AN");
            _countries.Add("NEW CALEDONIA", "NC");
            _countries.Add("NEW ZEALAND", "NZ");
            _countries.Add("NICARAGUA", "NI");
            _countries.Add("NIGER", "NE");
            _countries.Add("NIGERIA", "NG");
            _countries.Add("NIUE", "NU");
            _countries.Add("NORFOLK ISLAND", "NF");
            _countries.Add("NORTHERN MARIANA ISLANDS", "MP");
            _countries.Add("NORWAY", "NO");
            _countries.Add("OMAN", "OM");
            _countries.Add("PAKISTAN", "PK");
            _countries.Add("PALAU", "PW");
            _countries.Add("PALESTINIAN TERRITORY, OCCUPIED", "PS");
            _countries.Add("PANAMA", "PA");
            _countries.Add("PAPUA NEW GUINEA", "PG");
            _countries.Add("PARAGUAY", "PY");
            _countries.Add("PERU", "PE");
            _countries.Add("PHILIPPINES", "PH");
            _countries.Add("PITCAIRN", "PN");
            _countries.Add("POLAND", "PL");
            _countries.Add("PORTUGAL", "PT");
            _countries.Add("PUERTO RICO", "PR");
            _countries.Add("QATAR", "QA");
            _countries.Add("RÉUNION", "RE");
            _countries.Add("ROMANIA", "RO");
            _countries.Add("RUSSIAN FEDERATION", "RU");
            _countries.Add("RWANDA", "RW");
            _countries.Add("SAINT BARTHÉLEMY", "BL");
            _countries.Add("SAINT HELENA", "SH");
            _countries.Add("SAINT KITTS AND NEVIS", "KN");
            _countries.Add("SAINT LUCIA", "LC");
            _countries.Add("SAINT MARTIN", "MF");
            _countries.Add("SAINT PIERRE AND MIQUELON", "PM");
            _countries.Add("SAINT VINCENT AND THE GRENADINES", "VC");
            _countries.Add("SAMOA", "WS");
            _countries.Add("SAN MARINO", "SM");
            _countries.Add("SAO TOME AND PRINCIPE", "ST");
            _countries.Add("SAUDI ARABIA", "SA");
            _countries.Add("SENEGAL", "SN");
            _countries.Add("SERBIA", "RS");
            _countries.Add("SEYCHELLES", "SC");
            _countries.Add("SIERRA LEONE", "SL");
            _countries.Add("SINGAPORE", "SG");
            _countries.Add("SLOVAKIA", "SK");
            _countries.Add("SLOVENIA", "SI");
            _countries.Add("SOLOMON ISLANDS", "SB");
            _countries.Add("SOMALIA", "SO");
            _countries.Add("SOUTH AFRICA", "ZA");
            _countries.Add("SOUTH GEORGIA AND THE SOUTH SANDWICH ISLANDS", "GS");
            _countries.Add("SPAIN", "ES");
            _countries.Add("SRI LANKA", "LK");
            _countries.Add("SUDAN", "SD");
            _countries.Add("SURINAME", "SR");
            _countries.Add("SVALBARD AND JAN MAYEN", "SJ");
            _countries.Add("SWAZILAND", "SZ");
            _countries.Add("SWEDEN", "SE");
            _countries.Add("SWITZERLAND", "CH");
            _countries.Add("SYRIAN ARAB REPUBLIC", "SY");
            _countries.Add("TAIWAN, PROVINCE OF CHINA", "TW");
            _countries.Add("TAJIKISTAN", "TJ");
            _countries.Add("TANZANIA, UNITED REPUBLIC OF", "TZ");
            _countries.Add("THAILAND", "TH");
            _countries.Add("TIMOR-LESTE", "TL");
            _countries.Add("TOGO", "TG");
            _countries.Add("TOKELAU", "TK");
            _countries.Add("TONGA", "TO");
            _countries.Add("TRINIDAD AND TOBAGO", "TT");
            _countries.Add("TUNISIA", "TN");
            _countries.Add("TURKEY", "TR");
            _countries.Add("TURKMENISTAN", "TM");
            _countries.Add("TURKS AND CAICOS ISLANDS", "TC");
            _countries.Add("TUVALU", "TV");
            _countries.Add("UGANDA", "UG");
            _countries.Add("UKRAINE", "UA");
            _countries.Add("UNITED ARAB EMIRATES", "AE");
            _countries.Add("UNITED KINGDOM", "GB");
            _countries.Add("UNITED STATES", "US");
            _countries.Add("UNITED STATES MINOR OUTLYING ISLANDS", "UM");
            _countries.Add("URUGUAY", "UY");
            _countries.Add("UZBEKISTAN", "UZ");
            _countries.Add("VANUATU", "VU");
            _countries.Add("VATICAN CITY STATE", "see HOLY SEE");
            _countries.Add("VENEZUELA", "VE");
            _countries.Add("VIET NAM", "VN");
            _countries.Add("VIRGIN ISLANDS, BRITISH", "VG");
            _countries.Add("VIRGIN ISLANDS, U.S.", "VI");
            _countries.Add("WALLIS AND FUTUNA", "WF");
            _countries.Add("WESTERN SAHARA", "EH");
            _countries.Add("YEMEN", "YE");
            _countries.Add("ZAMBIA", "ZM");
            _countries.Add("ZIMBABWE", "ZW");


        }

        public static string GetPieUrl(Report report, string field, int width, int height, string backgroundColor)
        {
            string valueList = GetValueList(report.DataItems);
            string labelList = GetLabelList(report.DataItems, field);

            string pieUrl = "http://chart.apis.google.com/chart?cht=p3&chd=t:{2}&chs={0}x{1}&chl={3}&chf=bg,s,{4}";


            return string.Format(pieUrl, width, height, valueList, labelList, backgroundColor);
        }
        public static string GetMapUrl(Report report, string field, int width, int height, string backgroundColor)
        {
            List<ReportData> items = report.DataItems;

            items = FilterItemsByCountry(items);
            string valueList = GetValueList(items);
            string countryCodeList = GetCountryCodeList(items);

            string mapUrl = "http://chart.apis.google.com/chart?cht=t&chtm=world&chco=FFFFFF,FF0000,FFFF00,00FF00&chd=t:{2}&chld={3}&chs={0}x{1}&chf=bg,s,{4}";
            //EAF7FE

            return string.Format(mapUrl, width, height, valueList, countryCodeList, backgroundColor);
        }
        public static string GetLinearUrl(Report report, int width, int height, string backgroundColor)
        {
            string valueList = GetValueList(report.DataItems, report.MaxValue + (int) (report.MaxValue*0.5) );

            string pieUrl = "http://chart.apis.google.com/chart?chs={0}x{1}&cht=lc&chd=t:{2}&chf=bg,s,{3}";

            return string.Format(pieUrl, width, height, valueList, backgroundColor);    
        }

        private static string GetLabelList(List<ReportData> items, string field)
        {
            string labelList = string.Empty;

            foreach (ReportData item in items)
            {
                if (labelList != string.Empty)
                {
                    labelList += "|";
                }

                labelList += item[field];
            }

            return labelList;
        }
        private static string GetValueList(List<ReportData> items)
        {
            string valueList = string.Empty;
            
            foreach (ReportData item in items)
            {
                if (valueList != string.Empty)
                {
                    valueList += ",";
                }

                valueList += item.Value;
            }

            return valueList;
        }
        private static string GetValueList(List<ReportData> items, int maxValue)
        {
            string valueList = string.Empty;
            
            foreach (ReportData item in items)
            {
                if (valueList != string.Empty)
                {
                    valueList += ",";
                }


                valueList += (int)(((double)item.Value / maxValue) * 100);
            }

            return valueList;
        }        
        private static string GetCountryCodeList(List<ReportData> items)
        { 
            string countyCodeList = string.Empty;
            
            foreach (ReportData item in items)
            {       
                string country = item[DimensionType.Country.ToString()];         
                countyCodeList += _countries[country.ToUpper()];
            }

            return countyCodeList;
        }       
        private static List<ReportData> FilterItemsByCountry(List<ReportData> items)
        {
            List<ReportData> filteredItems = new List<ReportData>();

            foreach (ReportData item in items)
            {
                string country = item[DimensionType.Country.ToString()];

                if(_countries.ContainsKey(country.ToUpper()))
                {
                    filteredItems.Add(item);
                }
            }

            return filteredItems;
        }      
    }
}
